package com.example.fms.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.fms.dto.req.GoodsReqDto;
import com.example.fms.dto.req.UserReqDto;
import com.example.fms.dto.resp.GoodsRespDto;
import com.example.fms.dto.resp.GoodsRespOneDto;
import com.example.fms.dto.resp.UserRespDto;
import com.example.fms.service.GoodsService;
import com.example.fms.service.UserService;
import com.example.fms.vo.ResponseBean;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Resource
    private GoodsService goodsService;
    @GetMapping()
    public ResponseBean<Page<GoodsRespOneDto> > getGoodsPage(@RequestParam(value = "query", required = false) String query ,
                                                             @RequestParam(value = "pagenum", defaultValue = "1") Integer pagenum,
                                                             @RequestParam(value = "pagesize", defaultValue = "10") Integer pagesize) {
        GoodsReqDto goodsReqDto = new GoodsReqDto();
        goodsReqDto.setGoods_name(query);
        goodsReqDto.setPageNum(pagenum);
        goodsReqDto.setPageSize(pagesize);
        return ResponseBean.successMeat(goodsService.getGoodsPage(query,goodsReqDto));
    }

    @PostMapping()
    public ResponseBean<Boolean> saveGoods(@RequestBody GoodsReqDto req) {
        return ResponseBean.successMeat(goodsService.saveGoods(req));
    }

    @GetMapping("/{id}")
    public ResponseBean<GoodsRespOneDto> getGoodsById(@PathVariable String id) {
        return ResponseBean.successMeat(goodsService.getGoodsById(id));
    }

    @PutMapping("/{id}")
    public ResponseBean<Boolean> putGoodsById(@RequestBody GoodsReqDto req, @PathVariable Long id) {
        return ResponseBean.successMeat(goodsService.putGoodsById(req, id));
    }

    @DeleteMapping("/{id}")
    public ResponseBean<Boolean> deleteGoodsById(@PathVariable Long id) {
        return ResponseBean.successMeat(goodsService.deleteGoodsById(id));
    }

}
